package com.jiazhong.example.project.bookstore.commons.handler;

import com.jiazhong.example.project.bookstore.commons.util.JsonResult;
import com.jiazhong.example.project.bookstore.commons.util.ResultTool;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(UserSaveException.class)
    public JsonResult userSaveException(Throwable e) {
        return ResultTool.fail(590, e.getMessage());
    }

    @ExceptionHandler(UserUpdateException.class)
    public JsonResult userUpdateException(Throwable e) {
        return ResultTool.fail(591, e.getMessage());
    }

    @ExceptionHandler(UserNotFoundException.class)
    public JsonResult userNotFoundException(Throwable e, HttpServletResponse response) {
        response.setStatus(592);
        return ResultTool.fail(592, e.getMessage());
    }

    @ExceptionHandler(BookSaveException.class)
    public JsonResult bookSaveException(Throwable e, HttpServletResponse response) {
        response.setStatus(593);
        return ResultTool.fail(593, e.getMessage());
    }

    @ExceptionHandler(IOException.class)
    public JsonResult iOException(Throwable e, HttpServletResponse response) {
        response.setStatus(593);
        return ResultTool.fail(593, e.getMessage());
    }

    @ExceptionHandler(CategoryRemoveException.class)
    public JsonResult categoryRemoveException(Throwable e, HttpServletResponse response) {
        response.setStatus(594);
        return ResultTool.fail(594, e.getMessage());
    }

    @ExceptionHandler(UsernameNotFoundException.class)
    public JsonResult usernameNotFoundException(Throwable e, HttpServletResponse response) {
        response.setStatus(595);
        return ResultTool.fail(595, e.getMessage());
    }

    @ExceptionHandler(PasswordFailException.class)
    public JsonResult passwordFailException(Throwable e, HttpServletResponse response) {
        response.setStatus(596);
        return ResultTool.fail(596, e.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public JsonResult notLoginException(Throwable e, HttpServletResponse response) {
        response.setStatus(597);
        return ResultTool.fail(597, e.getMessage());
    }

    @ExceptionHandler(DiscountSaveOrUpdateException.class)
    public JsonResult discountSaveOrUpdateException(Throwable e, HttpServletResponse response) {
        response.setStatus(598);
        return ResultTool.fail(598, e.getMessage());
    }

    @ExceptionHandler(DiscountNotFoundException.class)
    public JsonResult discountNotFoundException(Throwable e, HttpServletResponse response) {
        //response.setStatus(599);
        return ResultTool.fail(599, e.getMessage());
    }

    @ExceptionHandler(ShoppingCartChangeNumberException.class)
    public JsonResult shoppingCartChangeNumberException(Throwable e, HttpServletResponse response) {
        //response.setStatus(599);
        return ResultTool.fail(600, e.getMessage());
    }

    @ExceptionHandler(OrderSaveException.class)
    public JsonResult orderSaveException(Throwable e, HttpServletResponse response) {
        return ResultTool.fail(601, e.getMessage());
    }
}
